We’ve reviewed different aspects of the car rental system and observed the attributes attached to the problem using various UML diagrams. Let’s explore the more practical side of things, where we will work on implementing the car rental system using multiple languages. This is usually the last step in an object-oriented design interview process.

We have chosen the following languages to write the skeleton code of the different classes present in the car rental system:

  • Java

  • C#

  • Python

  • C++

  • JavaScript

Car rental system classes#

In this section, we will provide the skeleton code of the classes designed in the class diagram lesson.

Note: For simplicity, we are not defining getter and setter functions. The reader can assume that all class attributes are private and accessed through their respective public getter methods and modified only through their public methods function.

Enumerations#

First, we will define all the enumerations required in the car rental system. According to the class diagram, there are seven enumerations used in the system, i.e., VehicleStatus, AccountStatus, ReservationStatus, PaymentStatus, VanType, CarType, and VehicleLogType. The code to implement these enumerations is as follows:

Note: JavaScript does not support enumerations, so we will use the Object.freeze() method as an alternative that freezes an object and prevents further modifications.

Enum definitions

Address, person, and driver#

This section contains the Address, Person, and Driver classes, where the first two classes are used as a custom data type. The implementation of these classes is shown below:se classes can be found below:

The Address, Person, and Driver classes

Account#

Account is an abstract class that represents the various people or actors that can interact with the system. There are two types of accounts: receptionist and customer. The implementation of Account and its subclasses is shown below:

Account and its derived classes

Vehicle#

Vehicle will be another abstract class, which serves as a parent for four different types of vehicles: Car, Van, Truck, and MotorCycle. The definition of the Vehicle and its child classes is given below:

Vehicle and its child classes

Equipment#

Equipment is an abstract class, and this section represents different equipment: Navigation, ChildSeat, and SkiRack added in the reservation. The code to implement these classes is shown below:

Equipment and its derived classes

Service#

Service is an abstract class, and this section represents different services: DriverService, RoadsideAssistance, and Wi-Fi added to the reservation. The code to implement these classes is shown below:

Service and its derived classes

Payment#

The Payment class is another abstract class, with the Cash and CreditCard classes as its child. This takes in the PaymentStatus enum to keep track of the payment status. The definition of this class is provided below:

Payment and its child classes

Vehicle log and Vehicle reservation#

VehicleLog is a class responsible for keeping track of all the events related to a vehicle. VehicleReservation is a class responsible for managing the reservation of vehicles. The implementation of this class is given below:

The VehicleLog and VehicleReservation classes

Notification#

The Notification class is another abstract class responsible for sending notifications, with the SMSNotification and EmailNotification classes as its child. The implementation of this class is shown below:

Notification and its derived classes

Parking stall and fine#

ParkingStall is a class used to locate vehicles in the car rental branch while the Fine class represents the fine applied on payment. The implementation of these classes is given below:

The Parking stall and fine classes

Search interface and vehicle catalog#

Search is an interface and the VehicleCatalog class is used to implement the search interface to help in vehicle searching. The code to perform this function is presented below:

The Search interface and the VehicleCatalog class

Car rental system and car rental branch#

The CarRentalSystem class is the base class of the system that is used to represent the whole car rental system (or the top-level classes of the system). CarRentalBranch represents the single branch of the system. The implementation of these classes is given below:

The CarRentalSystem and CarRentalBranch classes

Wrapping up#

We've explored the complete design of a car rental system in this chapter. We've looked at how a basic car rental system can be visualized using various UML diagrams and designed using object-oriented principles and design patterns.

Activity Diagram for the Car Rental System

Getting Ready: The ATM System